%%-------------------------------------------------------------------------------------------------------------------------------
%%-------------------------------------------------------------------------------------------------------------------------------
clear;
clc;
close all;
%cd D:\000_UIH_Work\520_FUDAN_PHD\450_US_System\201_GEN_Script\matlab_script;

format short g;             %% Set Data Disp Format

%% Global Parameter

GBL_MatlabSimModEN  = 0;      
GBL_FolderPath = './';
    %%-------------------------------------------------------------------------------------------------------------------------------
    %%-------------------------------------------------------------------------------------------------------------------------------
    %% Generate Scan Period Parameter
    %% User  Parameter, Input by Key-button

    %% Set 1: Trigger From Internal FPGA1
    %% Set 2: Trigger From Internal FPGA2
    %% Set 3: Trigger From Exteranl FPGA1
    %% Set 4: Trigger From Exteranl FPGA2    
    %% Set 5: Trigger From Exteranl FPGA3 -> FFPGA1/2, ACQ 128 Channel
    %% Set 6: Trigger From Internal FPGA3 -> FFPGA1/2, ACQ 128 Channel
    USR_EXTTriggerSlect         = 3;                        %% Set Last    

    USR_ACQ_SamleNum            = 4000;                     %% 4000 Samples
    USR_TX2RX_DelayUs           = 0 ;                      %% 1us

    USR_ExtTrigNumber           = 10;                   	%% 100-External Trigger, 1-Minitus, ACQ = 4 * 48 = 192 Second, 3Minuts
    USR_ExtTriggerPeriod        = 100 * 1e-3;               %% 100ms
    USR_TriggerDelay            = 300;                      %% 10us   
    USR_LineNumber              = 6;                        %% Constant

    USR_ImageMode               = 3;                        %%成像模式：1.单角度平面波，2.复合平面波，3.单发多收模式
   
     
    %% Explaintation
    %% 'E' = EOP, '0' = ZERO, '+' VPP, '-' VNN
    %% [23:21], [20:18], [17:15], [14:12], [11:09], [08:06], [05:03], [02:00]
    %%    +        +        +        +   _    -        -        -        -   
    %%    +        +        -        -   _    +        +        -        -  
    %%    +        -        +        -   _    +        -        +        -  
    %%   EOP
    USR_PulseWideCounter        = 35;                       %% Number of 5ns of "+"/"-", Eg: 40*5ns = 200ns, Perood = 400ns, Freq = 2.5M
                                                        %% USR_PulseWideCounter = SysTxFreq / TrandcuerFreq / 2
    USR_PulsePattern_Code       = '+-;_E';  
    USR_PatternRptCycleNum      = 0;                       %% Each Same NormalPulse will be Repeat 3 Times, could be set by User                                           
   
    %% Set ACQ Parameter
    USR_LaserACQ_Delay_Us       = 300;                      %% 300us Delay From LaserTR-Enable to ACQ 
    USR_ACQ_OP_LNA              = 21 ;              ...                 %% LNA Gain, 21dB, 18dB, 15dB       (3ddB Step)       ...
    USR_ACQ_OP_PGA              = 27 ;             ...                 %% PGA Gain: 18dB, 21dB, 24dB, 27dB (3ddB Step) ...                                          ...
    USR_ACQ_OP_Attenuation      = 0  ;             ...                 %% VCAT Attenuation, 0dB-36dB       (3ddB Step)  ...
    USR_ACQ_US_LNA              = 21 ;              ...                 %% LNA Gain, 21dB, 18dB, 15dB       (3ddB Step)       ...
    USR_ACQ_US_PGA              = 27 ;              ...                 %% PGA Gain: 18dB, 21dB, 24dB, 27dB (3ddB Step) ...                                          ...
    USR_ACQ_US_Attenuation      = 36 ;  

    %%-------------------------------------------------------------------------------------------------------------------------------                                                        
    %%-------------------------------------------------------------------------------------------------------------------------------
    %% Simulation mode, Reduce Data Size, for Matlab Display

    if (GBL_MatlabSimModEN == 1)
        
        USR_ACQ_SamleNum        = 400;                      %% 400 Samples
        USR_TX2RX_DelayUs       = 0.01;                     %% 0.01us

        USR_ExtTrigNumber       = 3;                        %% External Trigger = 3 Times
        USR_ExtTriggerPeriod    = 0.3 * 1e-3;               %% 200us, 2*6*8us = 96us
        USR_TriggerDelay        = 10;                       %% 10us 
        
        USR_LineNumber          = 7;                        %% Constant

        USR_PulseWideCounter    = 40;                       %% Number of 5ns of "+"/"-", 
        USR_PulsePattern_Code   = '++++_----; ++--_++--; +-+-_+-+-; _E'; 
        USR_PatternRptCycleNum  = 1;                        %% Each NormalPulse will be Repeat 3 Times, could be set by User

    end

%%-------------------------------------------------------------------------------------------------------------------------------
%%-------------------------------------------------------------------------------------------------------------------------------
%%-------------------------------------------------------------------------------------------------------------------------------
Result = fun_GenUltrasoundCmdScript                 ...
    (                                               ...
        GBL_FolderPath,                             ...
        GBL_MatlabSimModEN          ,               ...                                           ...
        USR_EXTTriggerSlect         ,               ...                                          ...
        USR_ACQ_SamleNum            ,               ...                 %% Number of Samples (40Msps Samples)
        USR_TX2RX_DelayUs           ,               ...                 %% TX2RX_Delay, Unit: us                                         ...
        USR_ExtTrigNumber           ,               ...            	    %% 100-External Trigger, 1-Minitus, ACQ = 4 * 48 = 192 Second, 3Minuts
        USR_ExtTriggerPeriod        ,               ...                 %% 100ms
        USR_TriggerDelay            ,               ...                 %% Trigger in term of us                               ...
        USR_LineNumber              ,               ...                 %% Constant, Number of Transimitt Line in Each External Period                             ...
        USR_PulseWideCounter        ,               ...                 %% Number of Pulse in 5ns, example: 1, 5ns Pulse, DivPeriod = 2 * 1
        USR_PulsePattern_Code       ,               ...
        USR_PatternRptCycleNum      ,               ...                 %% Each Same NormalPulse will be Repeat 3 Times, could be set by                                             
                                                    ...
        USR_ACQ_OP_LNA                 ,               ...                 %% LNA Gain, 21dB, 18dB, 15dB       (3ddB Step)       ...
        USR_ACQ_OP_PGA                 ,               ...                 %% PGA Gain: 18dB, 21dB, 24dB, 27dB (3ddB Step) ...                                          ...
        USR_ACQ_OP_Attenuation         ,               ...                 %% VCAT Attenuation, 0dB-36dB       (3ddB Step)  ...
        USR_ACQ_US_LNA                 ,               ...                 %% LNA Gain, 21dB, 18dB, 15dB       (3ddB Step)       ...
        USR_ACQ_US_PGA                 ,               ...                 %% PGA Gain: 18dB, 21dB, 24dB, 27dB (3ddB Step) ...                                          ...
        USR_ACQ_US_Attenuation         ,               ...                 %% VCAT Attenuation, 0dB-36dB       (3ddB Step)  ...
        USR_ImageMode                                ...                   %%成像模式：对应单角度平面波，复合平面波，单发多收模式
       );  
           